package cn.com.dashihui.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;

import cn.com.dashihui.kit.ValidateKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.SpikeList;
import cn.com.dashihui.web.service.SpikeListService;
import cn.com.dashihui.web.service.SpikeTimeService;

@RequiresAuthentication
public class SpikeListController extends BaseController {
	private SpikeListService spikeListService = new SpikeListService();
	private SpikeTimeService spikeTimeService = new SpikeTimeService();

	// @RequiresPermissions("spike:list:index")
	public void index() {
		render("index.jsp");
	}

	public void pageList() {
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, spikeListService.findTimeList(pageNum, pageSize));

	}

	// @RequiresPermissions("spike:list:index")
	public void page() {
		String title = getPara("title");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, spikeListService.findByPage(pageNum, pageSize, title));
	}

	public void page2() {
		int id = getParaToInt("sid");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, spikeListService.findGoodsByPage(pageNum, pageSize, id));
	}

	public void page3() {
		int id = getParaToInt("sid");
		String title = getPara("title");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, spikeListService.findOutSideGoodsByPage(pageNum, pageSize, id, title));
	}

	public void toSet() {
		int id = getParaToInt("listid");
		if (id != 0) {
			setAttr("slid", id);

		}
		render("listgoods.jsp");

	}

	// @RequiresPermissions("spike:list:edit")
	public void toEdit() {
		int id = getParaToInt(0, 0);
		if (id != 0) {
			/* 将查询 */
			setAttr("object", spikeListService.findById(id));
		}
		render("edit.jsp");
	}

	// @RequiresPermissions("spike:list:edit")
	public void doEdit() throws ParseException {

		String id = getPara("listid");
		String active_title = getPara("active_title");
		String active_state = getPara("active_state");
		String start = getPara("start");
		String stop = getPara("stop");
		int publish = getParaToInt("publish");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date dt1 = sf.parse(start);
		Date dt2 = sf.parse(stop);
		
		if (StrKit.isBlank(id)) {
			renderResult(1);
			return;
		} 
		if(StrKit.isBlank(active_title)){
			renderResult(2);
			return;
		}
		if(active_title.length()>20){
			renderResult(3);
			return;
		}
		if (dt1.after(dt2)) {
			renderResult(4);
			return;
		}

			SpikeList list = new SpikeList().set("id", id).set("active_title", active_title)
					.set("start", start).set("stop", stop).set("publish", publish);
			if (StrKit.notBlank(active_state)) {
				list.set("active_state", active_state);
			}
			if (spikeListService.editList(list)) {
				renderSuccess();
				return;
			}
		renderFailed();
	}

	// @RequiresPermissions("spike:list:add")
	public void toAdd() {
		setAttr("tl", spikeTimeService.findActiveSpikeTime());
		render("add.jsp");
	}

	// @RequiresPermissions("spike:list:add")
	public void doAdd() throws ParseException {
		String active_title = getPara("active_title");
		
		// int timeid = getParaToInt("tid");
		String[] timeid = getParaValues("tid");
		String t=StringUtils.join(timeid,",");
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
		String start=getPara("start");
		String stop=getPara("stop");
		int publish = getParaToInt("publish");
		//判断活动标题是否为空
		if (StrKit.isBlank(active_title)) {
			renderResult(1);
			return;
		} 
		if (active_title.length() > 20) {
			renderResult(2);
			return;
		} 
		if (StrKit.isBlank(start)) {
			renderResult(3);
			return;
		
		} if(StrKit.isBlank(stop)){
			renderResult(4);
			return;
		}
		if(f.parse(start).after(f.parse(stop))){
			renderResult(5);
			return;
		} 
		if (timeid==null||timeid.length==0) {
			renderResult(6);
			return;
		}
		   
			SpikeList spl = new SpikeList().set("timeid", t)
					.set("active_title", active_title).set("start", start).set("stop", stop).set("publish", publish);
			if (spikeListService.addSpikeList(spl)) {
				renderSuccess();
				return;
			}
		renderFailed();

	}

	// @RequiresPermissions("spike:list:delete")
	public void doDelete() {
		int id = getParaToInt(0, 0);
		if (id != 0 && spikeListService.delList(id)) {
			renderSuccess();
			return;
		}
		renderFailed();
	}

	// 列出商品的活动，并列出商品活动时间点
	/*
	 * public void toList() {
	 * 
	 * render("indexlist.jsp"); } public void pages() { //int id =
	 * getParaToInt("sid");
	 * 
	 * int pageNum = getParaToInt(0, 1); int pageSize = getParaToInt("pageSize",
	 * PropKit.getInt("constants.pageSize")); renderResult(0,
	 * spikeListService.findActiveListsByPage(pageNum, pageSize)); }
	 */

	public void toAddGoods() {

		int id = getParaToInt(0, 0);
		if (id != 0) {
			setAttr("lid", id);
		}
		render("addgoods.jsp");
	}

	public void doAddGoods() {
		String gid = getPara("gid");
		String[] checkcode = getParaValues("checkcode");

		if (checkcode != null && checkcode.length != 0) {
			spikeListService.addGoods(checkcode, gid);
			renderSuccess();
			return;
		}

		renderFailed();
	}

	public void doDeleteGoods() {
		int id = getParaToInt(0, 0);
		if (id != 0 && spikeListService.delGoods(id)) {
			renderSuccess();
			return;
		}
		renderFailed();
	}

	/*public void listTime() {
		renderJson(spikeTimeService.listTime());
	}*/

	public void doSort() {
		/*ValidateKit v = null;*/
		//Pattern pat=Pattern.compile(ValidateKit.V_UNPOSITIVE_FLOAT);
		//非负整数
		Pattern p=Pattern.compile("^[1-9]\\d*|0$");
		Map<String, String[]> params = getParaMap();
		if (params != null && params.size() != 0) {
			String keys = new String();
			String value = new String();
			Map<String, String> objMap = new HashMap<String, String>();
			for (Entry<String, String[]> entry : params.entrySet()) {
				value = entry.getValue()[0];
				keys = entry.getKey();

				if (StrKit.isBlank(value)) {
					value = "0";
				}
				if (keys.startsWith("orderNo") && value.length() > 3) {
					value = "0";

				}
				//判断秒杀价格是否为非负浮点数
				if(keys.startsWith("spikePrice")&&!ValidateKit.Unpositive_float(value)){
					renderResult(1);
					return;
				}
				//判断输入的秒杀数量是否是正整数
				if(keys.startsWith("spikeNum")&&!p.matcher(value).find()){
					renderResult(2);
					return;
					
				}
				//判断输入的限购数量是否为空，如果不为空则判断是否为非负正整数
				if(keys.startsWith("limited")&&!value.isEmpty()&&!p.matcher(value).find())
				{
					renderResult(3);
					return;
				}
				objMap.put(keys, value);
			}
			spikeListService.sortList(objMap);
			renderSuccess();
			return;
		}
		renderFailed();

	}
}
